
#include "user.h"
#include "usbd_cdc_if.h"
//#include <stdio.h>

static uint32_t led_blink_time_ms = 1000;
static uint8_t auto_out = 0;
static uint32_t io_count = 0;

/// 应用初始化
void app_init(void)
{
	
}

/// 创建静态线程
void create_thread(void)
{
	rt_thread_t thread20ms = rt_thread_create("20ms",
                            thread_20ms,
                            RT_NULL,
                            512,
                            RT_THREAD_PRIORITY_MAX-2, 
                            20); 
	if( thread20ms!=RT_NULL )
		rt_thread_startup(thread20ms);
	
	rt_thread_t oled = rt_thread_create("oled",
                            oled_thread,
                            RT_NULL,
                            512,
                            RT_THREAD_PRIORITY_MAX-1, 
                            20); 
	if( oled!=RT_NULL )
		rt_thread_startup(oled);
	
}

/// 20ms调度线程
void thread_20ms(void* param)  
{
	while(1)
	{
		rt_thread_mdelay(20);
		led_run();
		key_run();
		gpio_control();
	}
}

void oled_thread(void* param)
{
	OLED_Init();
	
	rt_thread_mdelay(100);
	OLED_Clear();
	rt_thread_mdelay(50);
	OLED_ShowString(0,0,"JT-PPK TEST",16);
	OLED_Refresh();
	rt_thread_mdelay(2000);
	
	OLED_Clear();
	rt_thread_mdelay(50);
	OLED_ShowString(0,0,"Enter Key...",16);
	OLED_Refresh();
	rt_thread_mdelay(100);
	
	while(1)
	{
		rt_thread_mdelay(50);
		oled_update();
	}
}

/// led运行函数
void led_run(void)
{
	static uint32_t count = 0;
	
	++count;
	
	if( count*20<led_blink_time_ms )
	{
		led_on();
	}else if( count*20<2*led_blink_time_ms )
	{
		led_off();
	}else
	{
		count = 0;
	}
}

/// 读取按键运行函数
void key_run(void)
{
	static uint8_t key_action = 0;  // 0-无动作，1-模糊按下,  2-完整按下
	
	if( GPIO_PIN_RESET==read_key() )
	{
		if( 0==key_action ) key_action = 1;
		else if( 1==key_action ) key_action = 2;
	}else if( GPIO_PIN_SET==read_key() && key_action==2 )
	{
		key_action = 0;
		auto_out = !auto_out;
	}
}

/// GPIO控制
void gpio_control(void)
{
	static char buff[128];
	static uint32_t send_tick_time = 0;
	static uint32_t action_time_ms = 0;
	static uint32_t loop_time_ms = 0;
	
	if( auto_out )
	{
		led_blink_time_ms = 100;    // 快闪
		
		if( rt_tick_get()-loop_time_ms>=800 )
		{
			io_out(0);
			++io_count;
			sprintf((char*)buff, "%d\n", io_count);
			CDC_Transmit_FS((uint8_t*)buff, strlen(buff));
			action_time_ms = rt_tick_get();	
			loop_time_ms = rt_tick_get();
		}
		
		if( rt_tick_get()-action_time_ms>=100 )
		{
			io_out(1);
		}
		
	}else{
		io_out(1);
		led_blink_time_ms = 1000;   //慢闪烁
	}
	
	if( rt_tick_get()-send_tick_time>10000 && rt_tick_get()-loop_time_ms>=1000 )
	{
		send_tick_time = rt_tick_get();
		sprintf(buff, "%d\n", io_count);
		CDC_Transmit_FS((uint8_t*)buff, strlen(buff));
	}
}

/// OLED显示更新
void oled_update(void)
{
	static uint32_t old_io_count = 0;
	static uint8_t buff[20];
	uint8_t space = 0;
	
	if( old_io_count!=io_count )
	{
		old_io_count = io_count;
		
		if( old_io_count<10ul ) space = 1;
		else if( old_io_count<100ul ) space = 2;
		else if( old_io_count<1000ul ) space = 3;
		else if( old_io_count<10000ul ) space = 4;
		else if( old_io_count<100000ul ) space = 5;
		else if( old_io_count<1000000ul ) space = 6;
		else if( old_io_count<10000000ul ) space = 7;
		else space = 12;
		
		space = (12-space)/2;		
		sprintf((char*)(buff+space), "%d", old_io_count);
		while( space-- ) buff[space] = ' ';
		
		OLED_Clear();
		rt_thread_mdelay(20);
		OLED_ShowString(0,0,buff,16);
		OLED_Refresh();
		//rt_thread_mdelay(1000);
	}
}
